/*
 * Copyright (C) 2011 ARNDroid
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.arndroid.lib.validation;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Class for constraint violations register.
 *
 * <p>
 * A tipical use is, in violations occurrency, create a ConstraintViolation (if necessary) and
 * add a violation register with {@link ConstraintViolations#addViolation addViolation} method.
 * </p>
 *
 * @author Alceu Rodrigues Neto
 */
public class ConstraintViolations {
	//Violations list:
	private ArrayList<String> violations;
	
	//Constructors:
	public ConstraintViolations() {
		violations = new ArrayList<String>();
	}
	
	/**
	 * A convenience constructor. Create with a inital violation registered.
	 * 
	 * @param initialViolation the inital violation
	 */
	public ConstraintViolations(String initialViolation) {
		this();
		addViolation(initialViolation);
	}
	
	/**
	 * Register a new violation in this object.
	 * 
	 * @param violation the new violation
	 * @return {@code true} if the object added 
	 */
	public boolean addViolation(String violation) {
		return violations.add(violation);
	}
	
	/**
	 * Return the number of violations registered.
	 * @return the number of violations registered
	 */
	public int size() {
		return violations.size();
	}
	
	/**
	 * Return a formated string containing all violations separated
	 * by a new line ({@code \n}). If there are no violations registered
	 * this method return {@code null}.
	 * @return a formated string or {@code null}
	 */
	public String formatedViolations() {
		StringBuilder result = new StringBuilder();
		if(size() > 0) {
			Iterator<String> iter = violations.iterator();
			//First element iteration:
			result.append(iter.next());
			//Other elements iteration if necessary:
			while(iter.hasNext()) {
				result.append('\n').
				       append(iter.next());
			}
			return result.toString();
		} else {
			return null;
		}
	}
	
	/**
	 * Return {@code true} if this ConstraintViolations contains a registered violation
	 * equals the informed violation.
	 * @param violation the violation to compare
	 * @return {@code true} if the violation already registered, otherwhise {@code false}
	 */
	public boolean contains(String violation) {
		String item;
		if(size() > 0) {
			Iterator<String> iter = violations.iterator();
			while(iter.hasNext()) {
				 item = iter.next();
				 if(item == null) {
					 if(violation == null)
						 return true;
				 }else if(item.equals(violation)) {
					 return true;
				 }
			}
		}
		return false;
	}
	
	/**
	 * Return {@code true} if this ConstraintViolation is empty.
	 * 
	 * @return {@code true} if empty, otherwhise {@code false}
	 */
	public boolean isEmpty() {
		return size() == 0;
	}
	
	/**
	 * Return a List with all violations registered. If there are no
	 * violations registered, return a empty list.
	 * 
	 * @return	the list of violations
	 */
	public List<String> getViolationsList() {
		return new ArrayList<String>(violations);
	}
}
